//
//  SIXDiscoverController.m
//  weibo
//
//  Created by chenangel on 16/5/20.
//  Copyright © 2016年 chuhan. All rights reserved.
//

#import "SIXDiscoverController.h"
#import "SIXAdvertisementView.h"
#import "SIXHotCell.h"
#import "SIXDiscoverCell.h"
#import "SIXDiscoverModel.h"
#import "SIXHotButton.h"
#import "SIXHotViewController.h"
#import "SIXBtn0Controller.h"

#import "SIXSearchTableViewController.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height

@interface SIXDiscoverController ()<UITableViewDataSource, UITableViewDelegate, SIXHotCellDelegate,UISearchBarDelegate>

/** tableView */
@property(nonatomic, weak) UITableView *tableView;
/** 组数据 */
@property(nonatomic, strong) NSMutableArray *groups;

#warning 测试用--点击cell可以跳转时删除
@property(nonatomic, weak) SIXDiscoverCell *cell;


@end

@implementation SIXDiscoverController
#pragma mark - 懒加载
- (NSMutableArray *)groups {
    if (_groups == nil) {
        _groups = [NSMutableArray array];
        
        NSArray *groupArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"discover.plist" ofType:nil]];
        
        for (NSArray *array in groupArray) {
            NSMutableArray *arrayM = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                [arrayM addObject:[SIXDiscoverModel discoverWithDict:dict]];
            }
            [_groups addObject:arrayM];
        }
        
    }
    return _groups;
}

// cell标识
static NSString *ID = @"hot";
static NSString *ID1 = @"discover";

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    
    self.tableView = tableView;
    
    // 设置头部视图
    SIXAdvertisementView *adView = [[[NSBundle mainBundle] loadNibNamed:@"SIXAdvertisementView" owner:nil options:nil] lastObject];
    self.tableView.tableHeaderView = adView;
    
    // 设置尾部视图--不显示无内容cell
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
    // 设置数据源和代理
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.y = 20;
    self.view.backgroundColor = [UIColor whiteColor];
    [UIApplication sharedApplication].statusBarHidden =YES;
    [self addSearchField];
}
#pragma mark--搜索框
- (void)addSearchField{
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, self.view.width, 44)];
    searchBar.delegate = self;
    [self.view addSubview: searchBar];
    
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [self search];
}

- (void)search{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MessageSearch" bundle:nil];
    SIXSearchTableViewController *searchVC = [storyboard instantiateInitialViewController];
    
    [self presentViewController:searchVC animated:YES completion:nil];
}
#pragma mark - 数据源方法
// 组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.groups.count + 1;
}

// 每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        NSArray *array = self.groups[section - 1];
        return array.count;
    }
}

// cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) { // 第一组
        SIXHotCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SIXHotCell" owner:nil options:nil] lastObject];
        }
        // 设置代理
        cell.delegate = self;
        
        
        [cell.btn0 setTitle:@"#小码哥教育#" forState:UIControlStateNormal];
        UIImage *image = [UIImage imageNamed:@"expand"];
        [cell.btn0 setImage: [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
        
        [cell.btn1 setTitle:@"#没有数据怎么搞#" forState:UIControlStateNormal];
        
        [cell.btn2 setTitle:@"#霍建华表白林心如#" forState:UIControlStateNormal];
        
        [cell.btn3 setTitle:@"热门话题" forState:UIControlStateNormal];
        
        
        return cell;
        
    }else { // 其他组
        SIXDiscoverCell *cell = [tableView dequeueReusableCellWithIdentifier:ID1];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SIXDiscoverCell" owner:nil options:nil] lastObject];
        }
        
        NSArray *array = self.groups[indexPath.section-1];
        // 取出模型数据
        SIXDiscoverModel *discover = array[indexPath.row];
        
        cell.cellImageView.image = [UIImage imageNamed:discover.icon];
        cell.titleLabel.text = discover.title;
        cell.detailLabel.text = discover.suggest;
        
        return cell;
    }
    
}


#pragma mark - 代理方法
// 设置组头部高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

// 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 80;
    }else {
        return 40;
    }
    
}

// 监听cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            SIXHotViewController *hotVc = [[SIXHotViewController alloc] init];
            [self.navigationController pushViewController:hotVc animated:YES];
        }else if (indexPath.row == 1) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }else if (indexPath.row == 1) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }else if (indexPath.row == 2) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }
        
    }else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }else if (indexPath.row == 1) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }else if (indexPath.row == 2) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }else if (indexPath.row == 3) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }else if (indexPath.row == 4) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }else if (indexPath.row == 5) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }else if (indexPath.row == 6) {
            self.cell = [tableView cellForRowAtIndexPath:indexPath];
            [self popTips:self.cell.titleLabel.text];
            
        }
        
    }
}

#pragma mark - SIXHotCellDelegate
- (void)hotCell:(SIXHotCell *)cell didClickBtn:(NSInteger)index {
    
    if (index == 0) {
        SIXBtn0Controller *btn0Vc = [[SIXBtn0Controller alloc] init];
        [self.navigationController pushViewController:btn0Vc animated:YES];
    }else if (index == 1) {
        [self popTips:cell.btn1.titleLabel.text];
    }else if (index == 2) {
        [self popTips:cell.btn2.titleLabel.text];
    }else if (index == 3) {
        [self popTips:cell.btn3.titleLabel.text];
    }
    
    
    //    switch (index) {
    //        case 0:
    //            NSLog(@"btnClick --- 0");
    //
    //            SIXBtn0Controller *btn0Vc = [[SIXBtn0Controller alloc] init];
    //            [self.navigationController pushViewController:btn0Vc animated:YES];
    //
    //            break;
    //
    //        case 1:
    //            NSLog(@"btnClick --- 1");
    //            break;
    //
    //        case 2:
    //            NSLog(@"btnClick --- 2");
    //            break;
    //
    //        case 3:
    //            NSLog(@"btnClick --- 3");
    //            break;
    //
    //        default:
    //            break;
    //    }
    
}

#warning  测试提示框(因无数据,故设此提示框,有数据时删除)
- (void)popTips:(NSString *)string {
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:string message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alertVc addAction:cancelAction];
    [self presentViewController:alertVc animated:YES completion:nil];
    
}

@end
